/**
 * Copyright &copy; 2012-2014 hongsuan.pplive All rights reserved.
 */
package com.hongsuan.pplive.modules.cms.web.front;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cnjson.common.config.Global;
import com.cnjson.db.persistence.Page;
import com.cnjson.common.utils.StringUtils;
import com.cnjson.common.web.BaseController;
import com.hongsuan.pplive.modules.cms.entity.Article;
import com.hongsuan.pplive.modules.cms.entity.Category;
import com.hongsuan.pplive.modules.cms.service.ArticleService;
import com.hongsuan.pplive.modules.sys.utils.UserUtils;

/**
 * 网站搜索Controller
 * @author ThinkGem
 * @version 2013-5-29
 */
@Controller
@RequestMapping(value = "${frontPath}/search")
public class FrontSearchController extends BaseController{
	
	@Autowired
	private ArticleService articleService;
	
	/**
	 * 全站搜索
	 */
	@RequestMapping(value = "")
	public String search(String t, @RequestParam(required=false) String q, @RequestParam(required=false) String qand, @RequestParam(required=false) String qnot, 
			@RequestParam(required=false) String a, @RequestParam(required=false) String cid, @RequestParam(required=false) String bd,
			@RequestParam(required=false) String ed, HttpServletRequest request, HttpServletResponse response, Model model) throws Exception{
		long start = System.currentTimeMillis();
		String artisan_id = request.getParameter(ARTISAN_ID);
		if(StringUtils.isBlank(artisan_id)){
			throw new Exception("手艺人信息为空");
		}
		String token = request.getParameter(USER_PARAMS_TOKEN);
		model.addAttribute(ARTISAN_ID,artisan_id);
		model.addAttribute(USER_PARAMS_TOKEN,token);
		// 重建索引（需要超级管理员权限）
		if ("cmd:reindex".equals(q)){
			if (UserUtils.getUser().isAdmin()){
				// 文章模型
				if (StringUtils.isBlank(t) || "article".equals(t)){
					articleService.createIndex();
				}
				model.addAttribute("message", "重建索引成功，共耗时 " + (System.currentTimeMillis() - start) + "毫秒。");
			}else{
				model.addAttribute("message", "你没有执行权限。");
			}
		}
		// 执行检索
		else{
			String qStr = StringUtils.replace(StringUtils.replace(q, "，", " "), ", ", " ");
			// 文章检索
			if (StringUtils.isBlank(t) || "article".equals(t)){
				Page<Article> page = articleService.search(new Page<Article>(1,1000), qStr, cid, bd, ed);
				page.setMessage("匹配结果，共耗时 " + (System.currentTimeMillis() - start) + "毫秒。");
				//过滤用户可看到的
				List<Article> arList = new ArrayList<Article>();
				if(CollectionUtils.isNotEmpty(page.getList())){
					List<String> idList = new ArrayList<String>(page.getList().size());
					for(Article article:page.getList()){
						if(article != null){
							idList.add(article.getId());
						}
					}
					String[] ids = new String[]{};
					ids = idList.toArray(ids);
					
					Article article = new Article(new Category());
					article.setIds(ids);
					article.setArtisan_id(artisan_id);
					arList = articleService.findListFornt(article );
				}
				model.addAttribute("arList", arList);
				//model.addAttribute("page", page);
			}
		}
		model.addAttribute("t", t);// 搜索类型
		model.addAttribute("q", q);// 搜索关键字
		model.addAttribute("qand", qand);// 包含以下全部的关键词
		model.addAttribute("qnot", qnot);// 不包含以下关键词
		model.addAttribute("cid", cid);// 搜索类型
		return "modules/cms/front/themes/"+ Global.getConfig("site.theme")+"/frontSearch";
	}
//	@RequestMapping(value = "/course")
//	public String searchPage(String t, @RequestParam(required=false) String searchWord, @RequestParam(required=false) String qand, @RequestParam(required=false) String qnot, 
//			@RequestParam(required=false) String a, @RequestParam(required=false) String cid, @RequestParam(required=false) String bd,
//			@RequestParam(required=false) String ed, HttpServletRequest request, HttpServletResponse response, Model model)throws Exception {
//		
//		String artisan_id = request.getParameter(ARTISAN_ID);
//		if(StringUtils.isBlank(artisan_id)){
//			throw new Exception("手艺人信息为空");
//		}
//		String token = request.getParameter(USER_PARAMS_TOKEN);
//		model.addAttribute(ARTISAN_ID,artisan_id);
//		model.addAttribute(USER_PARAMS_TOKEN,token);
//		
//		model.addAttribute("t", t);// 搜索类型
//		model.addAttribute("searchWord", searchWord);// 搜索关键字
//		model.addAttribute("qand", qand);// 包含以下全部的关键词
//		model.addAttribute("qnot", qnot);// 不包含以下关键词
//		model.addAttribute("cid", cid);// 搜索类型
//		
//		return "modules/cms/front/themes/"+ Global.getConfig("site.theme")+"/frontSearch";
//	}
	
}
